<?php
/**
 * @ScriptName: response.php
 * @Function: Defines an response component for all CakeTomato Controllers,
 * @Package: cake.tomato.controller.component,
 * @Insparation: tomato,
 * @Author: tomato at<wangshijun2010@gmail.com>,
 * @License: http://www.opensource.org/licenses/mit-license.php The MIT License
 * @CreateDate: 2009-10-29
 */
Class ResponseComponent extends Object {
    public $name = 'Response';
    public $charset = 'utf-8';
    public $settings = array(
        'var'=>'ResponseType',
        'json' => 'ResponseJSON'
    );
    public $controller = false;
    public $components = array('Json');
    private $type = false;

    public function initialize(&$controller, $settings=array()){
        $this->controller =& $controller;
        $this->settings = am($this->settings, $settings);
    }
    /* we must refresh the header content encoding type */
    public function charset($charset='utf-8'){
        $this->charset = $charset;
        $this->set_header();
    }
    /* Return json response */
    public function json(){
        Configure::write('debug', 0);
        $this->type = 'application/json';
        $this->controller->layout = 'ajax';
        $this->controller->set($this->settings['var'], 'json');
        $args = func_get_args();
        $this->controller->set($this->settings['json'], $this->encode($args));
        $this->set_header();
        $this->controller->render('json');
    }
    // which is used to return ajax html markups
    public function ajax(){
        Configure::write('debug', 0);
        $this->type = 'text/html';
        $this->controller->layout = 'ajax';
        $this->set_header();
    }
    public function html(){
        $this->type = 'text/html';
        $this->controller->set($this->settings['var'], 'html');
        $this->set_header();
    }
    public function script(){
        $this->type = 'application/x-javascript';
        $this->controller->set($this->settings['var'], 'script');
        $this->set_header();
    }
    public function xml(){
        $this->type = 'text/xml';
        $this->controller->set($this->settings['var'], 'xml');
        $this->set_header();
    }
    public function text(){
        $this->type = 'text/plain';
        $this->controller->set($this->settings['var'], 'text');
        $this->set_header();
    }
    public function file($params){

        extract($params);

        $fp = fopen($file, 'r');
        header('Content-Type: ' . $mimeType);
        header('Content-Length: '. $size);
        header('Content-Disposition: attachment; filename=' . $name . (empty($extension) ? '' : '.'.$extension));
        echo fread($fp,$size);
        fclose($file);
    }
    public function csv($content){
        Configure::write('debug', 0);
        header("Content-type: application/vnd.ms-excel");

        header("Content-disposition: csv" . date("Y-m-d") . ".csv");
        header("Content-disposition: filename=students.csv");
        print $content;
    }
    private function encode($args){
        $size = count($args);
        if($size < 4){
            $this->fill_args($args, $size);
        }
        $result->status = $args[0];
        $result->code = $args[1];
        $result->data = $args[2];
        $result->error = $args[3];
        return $this->Json->encode($result);
    }
    private function fill_args(&$args, $start){
        for($ii=$start; $ii<=3; $ii++){
            $args[$ii] = null;
        }
    }
    private function set_header(){
        header("Content-Type: $this->type; charset=$this->charset");
    }
}
?>